package com.huike.web.controller.clues;

import com.github.pagehelper.PageInfo;
import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.vo.FollowClueVo;
import com.huike.clues.service.ITbClueService;
import com.huike.clues.service.ITbFollowService;
import com.huike.common.constant.HttpStatus;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.domain.model.LoginUser;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.utils.SecurityUtils;
import com.huike.common.utils.ServletUtils;
import com.huike.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/clues/record")
public class TbFollowController extends BaseController {

    @Autowired(required = false)
    private ITbFollowService iTbFollowService;

    @Autowired
    private TokenService tokenService;

    @PostMapping
    public AjaxResult addFollowClue(@RequestBody FollowClueVo followClueVo){

        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String username = loginUser.getUsername();
        Boolean fs = iTbFollowService.addTrack(followClueVo, username);

        return fs?AjaxResult.success():AjaxResult.error();

    }

    @RequestMapping("/list")
    public TableDataInfo findClueTrack(Long clueId){

        List<TbClue> clueTrack = iTbFollowService.findClueTrack(clueId);

        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(clueTrack);
        rspData.setTotal(new PageInfo(clueTrack).getTotal());

        return rspData;
    }
}
